<template>
    <div class="balance_wp">
        <div class="newbalance_wrap">
            <div class="bl_box">
                <ul>
                    <li>
                        <p>{{ $t('m.sicbo.count.count[0]') }}</p>
                        <div class="e_value"><i>{{ roundCount.countAll }}</i></div>
                    </li>
                    <li>
                        <p>{{ $t('m.sicbo.count.count[1]') }}</p>
                        <div class="e_value"><i>{{ roundCount.count24 }}</i></div>
                    </li>
                    <li>
                        <p>{{ $t('m.sicbo.count.count[2]') }}</p>
                        <div class="e_value"><i>{{ roundCount.myBet }}</i></div>
                    </li>
                    <li>
                        <p>{{ $t('m.sicbo.count.count[3]') }}</p>
                        <div class="e_value"><i>{{ roundCount.myWin }}</i></div>
                    </li>
                    <div class="clBoth"></div>
                </ul>
            </div>
        </div>
        <div class="recordC">
            <p class="recordTitle">{{ $t('m.sicbo.count.count[4]') }}</p>
            <div style="position: relative">
                <table>
                    <thead>
                    <tr>
                        <th>{{ $t('m.sicbo.list.list[0]') }}</th>
                        <th>{{ $t('m.sicbo.list.list[2]') }}</th>
                        <th>{{ $t('m.sicbo.list.list[12]') }}</th>
                        <th>{{ $t('m.sicbo.list.list[8]') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="(item,k) in roundList" :key="k">
                        <td>Round#{{ item.rid }}</td>
                        <td>
                            <div class="luckDice">
                                <div :class="'dice dice_' + item.num1">
                                    <div :class="'dicePoint point_'+v" v-for="(v,k) in item.num1" :key="k"></div>
                                </div>
                                <div :class="'dice middle dice_'+item.num2">
                                    <div :class="'dicePoint point_'+v" v-for="(v,k) in item.num2" :key="k"></div>
                                </div>
                                <div :class="'dice dice_'+item.num3">
                                    <div :class="'dicePoint point_'+v" v-for="(v,k) in item.num3" :key="k"></div>
                                </div>
                                <div style="clear: both;"></div>
                            </div>
                        </td>
                        <td>
                            <template v-if="item.r1">
                                {{ $t('m.sicbo.list.list[13]') }}&nbsp;
                            </template>
                            <template v-else>
                                <template v-if="item.r2">
                                    {{ $t('m.sicbo.list.list[14]') }}&nbsp;
                                </template>
                                <template v-else>
                                    {{ $t('m.sicbo.list.list[15]') }}&nbsp;
                                </template>
                                <template v-if="item.r3">
                                    {{ $t('m.sicbo.list.list[16]') }}&nbsp;
                                </template>
                                <template v-else>
                                    {{ $t('m.sicbo.list.list[17]') }}&nbsp;
                                </template>
                            </template>
                        </td>
                        <td><span class="betNum">{{ item.eth }}</span></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</template>
<script>
    import "@/assets/css/count.scss";
    import axios from 'axios';
    import {getPlayer} from "@/assets/js/etherjs/sicbo";


    export default {
        name: 'sicbo_count',
        created() {
            // 按下安卓返回键后，让页面不返回
        },
        mounted() {
            this.init();
            this.aCpt({id: this._uid});
        },
        data() {
            return {
                request: {
                    pageNo: 1,
                    pageSize: 10,
                    isEnd: false,
                },
                roundCount: {
                    count24: 0,
                    countAll: 0,
                    myBet: 0,
                    myWin: 0
                },
                roundList: []
            }
        },
        methods: {
            init() {
                // 获取总投资
                axios.get('http://api.etscscan.com/client/sicbo/bet/count', {}).then((response) => {
                    if (response.data.error_code !== 0) {
                        throw new Error("get bet count error");
                    }
                    this.roundCount.count24 = parseInt(Number(response.data.data.count24) / 10 ** 9);
                    this.roundCount.countAll = parseInt(Number(response.data.data.countAll) / 10 ** 9);
                }).catch(function (error) {
                    console.log(error);
                });

                this.request.pageNo = 1;
                this.request.isEnd = false;
                this.request.list = [];
                this.getPlayerInfo();
                return this.getRoundList();
            },
            getRoundList: function () {
                if (this.request.isEnd) return new Promise((resolve) => {
                    resolve(1);
                });
                return axios.get('http://api.etscscan.com/client/sicbo/round', {
                    params: {
                        pageNo: this.request.pageNo,
                        pageSize: this.request.pageSize,
                    }
                }).then((response) => {
                    let roundList = response.data.data.list;
                    if (response.data.error_code !== 0) {
                        throw new Error("get round list error");
                    }
                    if (roundList.length < this.request.pageSize) {
                        this.request.isEnd = true;
                    }
                    for (let i in roundList) {
                        roundList[i].num1 = Number(roundList[i].res.substring(0, 1));
                        roundList[i].num2 = Number(roundList[i].res.substring(1, 2));
                        roundList[i].num3 = Number(roundList[i].res.substring(2, 3));
                        roundList[i].eth = parseInt((roundList[i].big + roundList[i].small + roundList[i].same + roundList[i].single + roundList[i].double) / 10 ** 9);
                        roundList[i].r1 = roundList[i].num1 == roundList[i].num2 && roundList[i].num1 == roundList[i].num3;
                        if (!roundList[i].r1) {
                            roundList[i].r2 = (roundList[i].num1 + roundList[i].num2 + roundList[i].num3) > 10;
                            roundList[i].r3 = (roundList[i].num1 + roundList[i].num2 + roundList[i].num3) % 2 == 0;
                        } else {
                            roundList[i].r2 = 0; //
                            roundList[i].r3 = 0; //
                        }
                    }
                    this.roundList = this.roundList.concat(roundList);

                }).catch((error) => {
                    console.log(error);
                });
            },
            getMore: function () {
                this.request.pageNo++;
                return this.getRoundList();
            },
            getPlayerInfo: async function () {
                let res = await getPlayer(this.walletActive.address);
                this.roundCount.myBet = parseInt(res[1].toString() / 10 ** 18);
                this.roundCount.myWin = parseInt(res[2].toString() / 10 ** 18);
            }
        }
    }
</script>
